//
//  XQHomeNavBar.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/2/3.
//

import UIKit
import Kingfisher

class XQHomeNavBar: UIView {
    
    var customerHandle: (() -> Void)?
  
    private lazy var logoImageView : UIImageView = {
        let imageView = UIImageView()
        imageView.layer.cornerRadius = 9.38
        imageView.layer.masksToBounds = true
        return imageView
    }()
    
    private lazy var logoLb : UILabel = {
        let lb = UILabel()
        lb.textColor = .white
        lb.font = UIFont.systemFont(ofSize: 28.6, weight: .medium)
        return lb
    }()
    
    private lazy var customerBtn : UIButton = {
        let btn = UIButton(type: .custom)
        btn.addTarget(self, action: #selector(customerClick), for: .touchUpInside)
        return btn
    }()
    
    var logo : String? = "" {
        didSet {
            if let url = URL(string: customerImageName ?? "") {
                self.logoImageView.kf.setImage(with: url,placeholder: UIImage(named: "AppIcon"))
            }else {
                self.logoImageView.image = UIImage(named: "AppIcon")
            }
        }
    }
    
    var title : String? = "" {
        didSet {
            self.logoLb.text = title
        }
    }
    
    var customerImageName : String? = "" {
        didSet {
            if let url = URL(string: customerImageName ?? "") {
                customerBtn.kf.setImage(with: url, for: .normal)
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.isUserInteractionEnabled = true
        makeUI()
        makeConstraints()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        makeUI()
        makeConstraints()
    }
    
    func makeUI() {
        self.addSubview(self.logoImageView)
        self.addSubview(self.logoLb)
        self.addSubview(self.customerBtn)
    }
    
    func makeConstraints() {
        self.logoImageView.snp.makeConstraints { make in
            make.top.left.bottom.equalTo(0)
            make.width.height.equalTo(48)
        }
        
        self.logoLb.snp.makeConstraints { make in
            make.centerY.equalTo(self.logoImageView)
            make.left.equalTo(self.logoImageView.snp.right).offset(9)
        }
        
        self.customerBtn.snp.makeConstraints { make in
            make.centerY.equalTo(self.logoImageView)
            make.right.equalTo(0)
            make.width.height.equalTo(32)
        }
    }
    
    @objc private func customerClick() {
        customerHandle?()
    }
    
}
